package com.ck.core.util;

import org.apache.commons.collections.CollectionUtils;

import java.util.List;

/**
 * 字符串工具类
 *
 * @author dxy
 * @date 2018/6/22 17:21
 */
public class StringUtils {
	/**
	 * 列表转化为格式化字符串
	 *
	 * @param list   List<Object>
	 * @param format 间隔符
	 * @return String(格式化字符串)
	 */
	public static String listToFormatString(List<?> list, String format) {
		if (CollectionUtils.isEmpty(list) || org.apache.commons.lang3.StringUtils.isBlank(format)) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			if (i > 0) {
				sb.append(format);
			}
			sb.append(list.get(i));
		}
		return sb.toString();
	}

	public static String hideMobile(String mobile){
		if(org.apache.commons.lang3.StringUtils.isBlank(mobile)){
			return "";
		}
		int length =mobile.length();
		// 不是11位的手机号码直接返回原串
		int mobilePhoneNumber = 11;
		if (length != mobilePhoneNumber) {
			return mobile;
		}
		StringBuilder sb = new StringBuilder();
		int start=3;
		int end = length -5;
		for(int i=0;i<mobile.length();i++){
			if(i>=start && i<=end){
				sb.append("*");
			}else{
				sb.append(mobile.charAt(i));
			}
		}
		return sb.toString();
	}

	public static boolean isLetterDigit(String str) {
		if ( org.apache.commons.lang3.StringUtils.isBlank(str) ) {
			return false;
		}
		String regex = "^[a-z0-9A-Z]+$";
		return str.matches(regex);
	}
}
